package com.starblink.imgsearch.result.ui.cell

import android.content.res.ColorStateList
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.starblink.android.basic.adapter.BaseBindingVH
import com.starblink.android.basic.ext.*
import com.starblink.android.basic.extension.apollo.id2String
import com.starblink.android.basic.extension.dp2px
import com.starblink.android.basic.extension.extraOf
import com.starblink.android.basic.extension.navigationTo
import com.starblink.android.basic.sensorsdata.extension.trackData
import com.starblink.android.basic.sensorsdata.room.entity.SkAntEntity
import com.starblink.android.basic.sensorsdata.spm.GTrackerAssistUtils
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmElementDef
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.android.basic.sensorsdata.spm.impl.SpmTrackHandler
import com.starblink.android.basic.util.ImageUtils
import com.starblink.basic.route.RoutePage
import com.starblink.basic.style.color
import com.starblink.basic.style.view.recyclerview.SimpleGapItemDecor
import com.starblink.basic.style.view.round.CornerType
import com.starblink.basic.util.ActivityStackManage
import com.starblink.basic.util.CommUtils
import com.starblink.imgsearch.databinding.CellForyouTitleBinding
import com.starblink.imgsearch.databinding.CellResultTipBinding
import com.starblink.imgsearch.databinding.CellSeeeMoreBinding
import com.starblink.imgsearch.databinding.ItemSearchBrandBinding
import com.starblink.imgsearch.result.data.ForYouTitleModel
import com.starblink.imgsearch.result.data.ResultBrandModel
import com.starblink.imgsearch.result.data.ResultTipModel
import com.starblink.imgsearch.result.data.SeeMoreItemModel
import com.starblink.rocketreserver.fragment.BrandVOF
import com.starblink.rocketreserver.fragment.CategoryF
import com.starblink.rocketreserver.fragment.PicSearchResultF

/**
 * 查看更多
 */
class SeeMoreCell(binding: CellSeeeMoreBinding, onClick: (SeeMoreCell.() -> Unit)? = null) :
    BaseBindingVH<SeeMoreItemModel, CellSeeeMoreBinding>(binding) {

    init {
        itemView.layoutParams = StaggeredGridLayoutManager.LayoutParams(
            StaggeredGridLayoutManager.LayoutParams.MATCH_PARENT,
            StaggeredGridLayoutManager.LayoutParams.WRAP_CONTENT
        ).apply {
            isFullSpan = true
        }
        binding.btn.click {
            onClick?.invoke(this)
        }
    }


    override fun onBind(model: SeeMoreItemModel, position: Int) {
        binding.btn.isEnabled = model.enable
        itemView.visibleOrGone(model.visible) {

        }
    }

}


class BrandChildCell(var proxyClick: (p: Int) -> Unit, binding: ItemSearchBrandBinding) :
    BaseBindingVH<ResultBrandModel, ItemSearchBrandBinding>(binding) {
    override fun onBind(model: ResultBrandModel, position: Int) {

        if (model.selected) {
            binding.cameraBrandItem.setBackgroundColor(color(com.starblink.basic.style.R.color.sk_gray800))
            binding.cameraBrandItem.setCornerRadius(
                color(com.starblink.basic.style.R.color.sk_gray800).toFloat(),
                CornerType.R_BORDER_COLOR
            )
            binding.txt.setTextColor(color(com.starblink.basic.style.R.color.sk_gray0))
        } else {
            binding.cameraBrandItem.setBackgroundColor(color(com.starblink.basic.style.R.color.sk_gray100))
            binding.cameraBrandItem.setCornerRadius(
                color(com.starblink.basic.style.R.color.sk_gray200).toFloat(),
                CornerType.R_BORDER_COLOR
            )
            binding.txt.setTextColor(color(com.starblink.basic.style.R.color.sk_gray600))
        }

        binding.txt.text = model.brandTxt
        binding.cameraBrandItem.click {
            if (position == 0) {
                SpmTrackHandler.addSingleTrackData(
                    SkAntEntity(
                        spmCnt = GTrackerAssistUtils.fetchElementValue(
                            SpmPageDef.GdsAndPostItemTab,
                            SpmElementDef.Element_33049
                        ),
                    )
                )
            } else {
                SpmTrackHandler.addSingleTrackData(
                    SkAntEntity(
                        spmCnt = GTrackerAssistUtils.fetchElementValue(
                            SpmPageDef.GdsAndPostItemTab,
                            SpmElementDef.Element_33049
                        ),
                        params = mapOf("filterName" to model.brandTxt)
                    )
                )
            }
            if (model.selected && position == 0) {
                return@click
            }
            proxyClick.invoke(position)
        }
    }
}


/**
 * 推荐标题
 */
class ForYouTitleCell(binding: CellForyouTitleBinding) :
    BaseBindingVH<ForYouTitleModel, CellForyouTitleBinding>(binding) {

    init {
        itemView.layoutParams = StaggeredGridLayoutManager.LayoutParams(
            StaggeredGridLayoutManager.LayoutParams.MATCH_PARENT,
            StaggeredGridLayoutManager.LayoutParams.WRAP_CONTENT
        ).apply {
            isFullSpan = true
        }
    }

    override fun onBind(model: ForYouTitleModel, position: Int) {
        itemView.visibleOrGone(model.visible) {

        }
    }

}


class ResultTipCell(binding: CellResultTipBinding) :
    BaseBindingVH<ResultTipModel, CellResultTipBinding>(binding) {

    init {
        itemView.setTag(SimpleGapItemDecor.VIEW_TAG_NO_EDGE, true)
        itemView.layoutParams = StaggeredGridLayoutManager.LayoutParams(
            StaggeredGridLayoutManager.LayoutParams.MATCH_PARENT,
            StaggeredGridLayoutManager.LayoutParams.WRAP_CONTENT
        ).apply {
            isFullSpan = true
        }
    }

    override fun onBind(model: ResultTipModel, position: Int) {
        itemView.visibleOrGone(model.visible) {
            when {
                model.firstScore > 0.8 -> {
                    binding.tv.text = string(com.starblink.basic.style.R.string.img_res_tip1)
                    binding.tv.setTextColor(color(com.starblink.basic.style.R.color.sk_green600))
                    binding.tv.setStrokeColorResource(com.starblink.basic.style.R.color.sk_green100)
                    binding.tv.backgroundTintList =
                        ColorStateList.valueOf(color(com.starblink.basic.style.R.color.sk_green50))
                }

                model.firstScore > 0.7 -> {
                    binding.tv.text = string(com.starblink.basic.style.R.string.img_res_tip2)
                    binding.tv.setTextColor(color(com.starblink.basic.style.R.color.sk_blue600))
                    binding.tv.setStrokeColorResource(com.starblink.basic.style.R.color.sk_blue100)
                    binding.tv.backgroundTintList =
                        ColorStateList.valueOf(color(com.starblink.basic.style.R.color.sk_blue50))
                }

                else -> {
                    binding.tv.text = string(com.starblink.basic.style.R.string.img_res_tip3)
                    binding.tv.setTextColor(color(com.starblink.basic.style.R.color.sk_yellow600))
                    binding.tv.setStrokeColorResource(com.starblink.basic.style.R.color.sk_yellow100)
                    binding.tv.backgroundTintList =
                        ColorStateList.valueOf(color(com.starblink.basic.style.R.color.sk_yellow50))
                }
            }
        }

    }

}